package org.springblade.modules.admin.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tenant.annotation.TenantIgnore;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.plan.PlanContentExcelSearchDTO;
import org.springblade.modules.admin.entity.PlanHistoryImage;
import org.springblade.modules.admin.service.PlanContentExcelService;
import org.springblade.modules.admin.service.PlanHistoryImageService;
import org.springblade.modules.admin.vo.image.by.ByPlanHistoryImgVO;
import org.springblade.modules.admin.vo.plan.PerformanceVoV2;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Date 15:23 2024/6/4
 * @Description {
 *   工单图片AI处理结果
 * }
 * @Author lsay
 **/
@RestController
@AllArgsConstructor
@RequestMapping("admin/plan/image/history")
public class PlanImageHistoryController {

	private final PlanHistoryImageService planHistoryImageService;

	@GetMapping("/count")
	public R<Integer> selectPageCount(PlanHistoryImage entity){
		return R.data(planHistoryImageService.selectPageCount(entity));
	}

	@GetMapping("/page")
	public R<IPage<PlanHistoryImage>> selectPage(PlanHistoryImage entity,Query query){
		return R.data(planHistoryImageService.selectPage(Condition.getPage(query),entity));
	}

	@GetMapping("/list")
	public R<List<PlanHistoryImage>> selectList(PlanHistoryImage entity){
		return R.data(planHistoryImageService.selectList(entity));
	}

	@GetMapping("/detail")
	public R<PlanHistoryImage> selectDetail(Long id,String url){
		if(Func.isEmpty(id) && Func.isEmpty(url)){
			throw new ServiceException("查询条件不可为空");
		}
		return R.data(planHistoryImageService.selectDetail(id,url));
	}

	@PutMapping("/submit")
	public R<Boolean> submit(@RequestBody PlanHistoryImage entity){
		return R.data(planHistoryImageService.saveOrUpdate(entity));
	}

	@PutMapping("/update/v2")
	public R<Boolean> updateV2(@RequestBody PlanHistoryImage entity){
		return R.data(planHistoryImageService.update(entity,Wrappers.<PlanHistoryImage>lambdaUpdate().eq(PlanHistoryImage::getUrl,entity.getUrl())));
	}

	@PutMapping("/update/audit")
	public R<Boolean> updateAudit(@RequestBody PlanHistoryImage entity){
		entity.setAuditTime(DateUtil.date());
		entity.setUpdateUser(AuthUtil.getUserId());
		return R.data(planHistoryImageService.update(entity,Wrappers.<PlanHistoryImage>lambdaUpdate().eq(PlanHistoryImage::getUrl,entity.getUrl())));
	}

	@GetMapping("/list/ai")
	public R<List<PlanHistoryImage>> selectListAi(String startDate,String endDate){
		return R.data(planHistoryImageService.selectListAi(startDate,endDate));
	}

	@TenantIgnore
	@GetMapping("/by/list/all")
	public R<List<ByPlanHistoryImgVO>> selectByHistoryImgList(String startDate,String endDate){
		return R.data(planHistoryImageService.selectByHistoryImgList(startDate,endDate));
	}

	@TenantIgnore
	@GetMapping("/by/sign/list/all")
	public R<List<ByPlanHistoryImgVO>> selectBySignHistoryImgList(String startDate,String endDate){
		return R.data(planHistoryImageService.selectBySignHistoryImgList(startDate,endDate));
	}

	@TenantIgnore
	@GetMapping("/call/back/list/all")
	public R<List<ByPlanHistoryImgVO>> selectCallBackImgList(String startDate,String endDate){
		return R.data(planHistoryImageService.selectCallBackImgList(startDate,endDate));
	}

}
